/*! \file scrollbar.h	
	This is the header file for scrollbar.cpp
	
	This contains the Scrollbar class
*/

#ifndef SCROLLBAR_H
#define SCROLLBAR_H

#include "container.h"
#include "button.h"
#include "imgbtn.h"
#include "image.h"
#include "listener.h"

/**
	\class Scrollbar

	This is a container with an upButton, a downButton, and a slider (that you can drag to scroll).
	
	A Scrollbar is just used by a Scrollpanel, because it needs something else to calculate its
	maxPosition.
	
	Note: when dragging the slider, draggingSlider is set, but we use the dragX and dragY from Container
	since they won't be used ("draggable" will always be set to false for a Scrollbar)
*/
class Scrollbar : public Container, public MouseListener { 
private:
	int topButtonID;
	int bottomButtonID;
	int scrollbarID;
	ImageButton* upButton;
	ImageButton* downButton;
	ImageButton* slider;
	int sliderPosition; // this represents how many pixels the window is being scrolled, so it could be like 5000
	bool draggingSlider;
	bool upButtonHeld; // if this is true, then the bar will continuously scroll in the update() function
	bool downButtonHeld;
	int lengthHeld; // this is the length that the button has been held for, when it reaches some number, it goes to 0 and the bar scrolls
	int draggedFrom; // represents where the slider was when you started dragging it
	int topMost; // this is the pixel location that corresponds to sliderPosition 0 (it is where the track begins)
	int maxPosition; // this is the location (not in pixels) that represents the highest sliderPosition possible

	int trackSize; // the size of the space between the top and bottom button in pixels

	int scrollUnit; // how much scrollPosition should change if you click one of the arrows
	int scrollPage; // how much scrollPosition should change if you click the bar

	int getScreenCoordOfSliderPosition() { return (int)(( (float)sliderPosition / ( maxPosition + ( maxPosition == 0 ) ) ) * (trackSize-slider->getHeight()));}
public:
	Scrollbar( Component* parent, int topID, int bottomID, int scrollID );

	/**
	* You can't actually set the width of scrollbar, since that's determined by the size of the
	* image used for the upButton, but changing the height is done here.
	*/
	void setSize( int newW, int newH );

	/**
	* This just calls setSize
	*/
	void setHeight( int newH );

	/**
	* This just calls setSize
	*/
	void setWidth( int newW );

	/**
	* This will scroll to the position specified in pixels relative to the top of the component.
	* So 0 is the absolute top of the component, but it is where the upButton is, so an argument
	* from 0 to upButton->getHeight() would set the slider to the uppermost position
	*/
	void scrollToPixel( int pixelY );

	/**
	* This sets the slider to the maximum position
	*/
	void scrollToBottom();
	
	/**
	* This takes whatever the sliderPosition is and actually moves the slider Button to that position
	*/
	void updateScroll();
	
	/**
	* This returns how big the track is. The track is the area between the up and down buttons.
	*/
	int getTrackSize();
	
	/**
	* This sets the new maxPosition. It is a pixel-measurement that represents how far up something can be
	* scrolled (so this could go to 5000 or something).
	*/
	void setMaxPosition( int newPosition );
	
	/**
	* Basic accessor
	*/
	int getMaxPosition();
	
	/**
	* Basic accessor - returns the slider Button
	*/
	ImageButton* getSlider();

	/**
	* This scrolls by however many units are specified. A unit is user-defined with setScrollUnit.
	* Positive numbers scroll up, negative numbers scroll down.
	*/
	void scrollByUnits( int numUnits = 1 );

	/**
	* Basic accessor
	*/
	int getSliderPosition();

	/**
	* This changes how many pixels are scrolled at a time whenever a function uses "units"
	*/
	void setScrollUnit( int su );
	
	/** 
	* This changes how many pixels are scrolled at a time whenevr a function uses "pages"
	*/
	void setScrollPage( int sp );
	
	/**
	* Returns how many pixels a "unit" represents
	*/
	int getScrollUnit();
	
	/**
	* Returns how many pixels a "page" represents
	*/
	int getScrollPage();
	
	/**
	* This is used for continuous scrolling (if you've held down the up or down button)
	*/
	virtual void update();
	
	/**
	* Draws the Scrollbar
	*/
	void draw();
		
	virtual void mouseDown(Event e);
	virtual void mouseUp(Event e);
	virtual void onScroll(Event e);
	virtual void onEnter(Event e);
	virtual void onLeave(Event e);
	
	virtual void mouseMove( Event e );
	
};
#endif
